package com.great.controller;

import com.github.pagehelper.PageInfo;
import com.great.domain.User;
import com.great.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

@SessionAttributes(value = {"user"})
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("searchuser")
    public ModelAndView searchuser(@RequestParam(name = "page", required = true,defaultValue = "1") int page,@RequestParam(name = "size",required = true,defaultValue = "5") int size,
                                   String keyword){
        ModelAndView mv = new ModelAndView();
        List<User> users = userService.searchuser(page, size,keyword);
        PageInfo pageInfo = new PageInfo(users);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("manage/user-list");
        return mv;
    }
    @RequestMapping("/delmore")
    public String delmore(String tag){
        String[] strs = tag.split(",");
        for(int i=0;i<strs.length;i++){
            userService.deluser(strs[i]);
        }
        return "redirect:/user/searchuser";
    }

    @RequestMapping("/adduser")
    public String adduser(User user){
        userService.adduser(user);
        return "redirect:/user/searchuser";
    }

    @RequestMapping("/open")
    public String openuser(String tag){
        String[] strs = tag.split(",");
        for(int i=0;i<strs.length;i++){
            userService.openuser(strs[i]);
        }
        return "redirect:/user/searchuser";
    }

    @RequestMapping("/close")
    public String closeuser(String tag){
        String[] strs = tag.split(",");
        for(int i=0;i<strs.length;i++){
            userService.closeuser(strs[i]);
        }
        return "redirect:/user/searchuser";
    }

    @RequestMapping("/findById")
    public ModelAndView findById(String id){
        ModelAndView mv = new ModelAndView();
        User users = userService.finById(id);
        mv.addObject("users",users);
        mv.setViewName("manage/user-change");
        return mv;
    }

    @RequestMapping("/updateById")
    public String updateById(User user){
        userService.updateById(user);
        return "redirect:/user/searchuser";
    }

    @RequestMapping("/usernamecheck")
    public void usernamecheck(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String user_name = request.getParameter("user_name");
        int count = userService.usernamecheck(user_name);
        if(count>0){
            response.getWriter().println(1);
        }else{
            response.getWriter().println(0);
        }

    }

    @RequestMapping("/regeiste")
    public String regeiste(User user){
        userService.adduser(user);
        return "login";
    }

    @RequestMapping("/reforward")
    public ModelAndView reforward(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("regesite");
        return mv;
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request,Model model,String user_Name, String user_Password,String verifyCodes){
        Object verifyCode = request.getSession().getAttribute("verify_code");
        System.out.println(verifyCode);
        System.out.println(verifyCodes);
        if (verifyCode.toString().toUpperCase().equals(verifyCodes.toUpperCase())||verifyCode.toString().toLowerCase(Locale.ROOT).equals(verifyCodes.toLowerCase(Locale.ROOT))) {
            int count = userService.usernamecheck(user_Name);
            if(count>0){
                User user = userService.selectbyname(user_Name,user_Password);
                if(user==null){
                    model.addAttribute("errMsg","密码错误");
                    return "login";
                }else{
                    if (user.getUser_Status()!=0) {
                        model.addAttribute("user",user);
                        return "redirect:/index/base";
                    }else{
                        model.addAttribute("errMsg","用户状态异常，已被封禁!!!若有异议,请联系客服!!");
                        return "login";
                    }
                }
            }else{
                model.addAttribute("errMsg","用户名不存在");
                return "login";
            }
        }else {
            model.addAttribute("errMsg","验证码错误");
            return "login";
        }

    }
    @RequestMapping("/Logout")
    public String Logout(SessionStatus sessionStatus){
        sessionStatus.setComplete();
        return "redirect:/index/base";
    }

    @RequestMapping("/userselfchange")
    public String userselfchange(User user){
        userService.updateById(user);
        return "redirect:/pages/changedata.jsp";
    }


}
